<?php require_once('../Connections/morroco.php'); ?>
<?php
if (!isset($_SESSION)) {
  session_start();
}
$MM_authorizedUsers = "";
$MM_donotCheckaccess = "true";

// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) { 
  // For security, start by assuming the visitor is NOT authorized. 
  $isValid = False; 

  // When a visitor has logged into this site, the Session variable MM_Username set equal to their username. 
  // Therefore, we know that a user is NOT logged in if that Session variable is blank. 
  if (!empty($UserName)) { 
    // Besides being logged in, you may restrict access to only certain users based on an ID established when they login. 
    // Parse the strings into arrays. 
    $arrUsers = Explode(",", $strUsers); 
    $arrGroups = Explode(",", $strGroups); 
    if (in_array($UserName, $arrUsers)) { 
      $isValid = true; 
    } 
    // Or, you may restrict access to only certain users based on their username. 
    if (in_array($UserGroup, $arrGroups)) { 
      $isValid = true; 
    } 
    if (($strUsers == "") && true) { 
      $isValid = true; 
    } 
  } 
  return $isValid; 
}

$MM_restrictGoTo = "../index.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {   
  $MM_qsChar = "?";
  $MM_referrer = $_SERVER['PHP_SELF'];
  if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
  if (isset($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING']) > 0) 
  $MM_referrer .= "?" . $_SERVER['QUERY_STRING'];
  $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
  header("Location: ". $MM_restrictGoTo); 
  exit;
}
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

// *** Redirect if username exists
$MM_flag="MM_insert";
if (isset($_POST[$MM_flag])) {
  $MM_dupKeyRedirect="agregar.php";
  $loginUsername = $_POST['nombre'];
  $LoginRS__query = sprintf("SELECT nombre FROM docente WHERE nombre=%s", GetSQLValueString($loginUsername, "text"));
  mysql_select_db($database_morroco, $morroco);
  $LoginRS=mysql_query($LoginRS__query, $morroco) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);

  //if there is a row in the database, the username was found - can not add the requested username
  if($loginFoundUser){
    $MM_qsChar = "?";
    //append the username to the redirect page
    if (substr_count($MM_dupKeyRedirect,"?") >=1) $MM_qsChar = "&";
    $MM_dupKeyRedirect = $MM_dupKeyRedirect . $MM_qsChar ."requsername=".$loginUsername;
    header ("Location: $MM_dupKeyRedirect");
    exit;
  }
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO docente (ID, nombre, id_usuario) VALUES (%s, %s, %s)",
                       GetSQLValueString($_POST['ID'], "int"),
                       GetSQLValueString($_POST['nombre'], "text"),
                       GetSQLValueString($_POST['id_usuario'], "int"));

  mysql_select_db($database_morroco, $morroco);
  $Result1 = mysql_query($insertSQL, $morroco) or die(mysql_error());

  $insertGoTo = "agregar.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

mysql_select_db($database_morroco, $morroco);
$query_max_id = "SELECT MAX(docente.ID) FROM docente";
$max_id = mysql_query($query_max_id, $morroco) or die(mysql_error());
$row_max_id = mysql_fetch_assoc($max_id);
$totalRows_max_id = mysql_num_rows($max_id);

$colname_datos_usuario = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_datos_usuario = $_SESSION['MM_Username'];
}
mysql_select_db($database_morroco, $morroco);
$query_datos_usuario = sprintf("SELECT * FROM usuarios WHERE nombre = %s", GetSQLValueString($colname_datos_usuario, "text"));
$datos_usuario = mysql_query($query_datos_usuario, $morroco) or die(mysql_error());
$row_datos_usuario = mysql_fetch_assoc($datos_usuario);
$totalRows_datos_usuario = mysql_num_rows($datos_usuario);

$colname_docentes = "-1";
if (isset($row_datos_usuario['ID'])) {
  $colname_docentes = $row_datos_usuario['ID'];
}
mysql_select_db($database_morroco, $morroco);
$query_docentes = sprintf("SELECT * FROM docente WHERE id_usuario = %s  ORDER BY docente.nombre", GetSQLValueString($colname_docentes, "int"));
$docentes = mysql_query($query_docentes, $morroco) or die(mysql_error());
$row_docentes = mysql_fetch_assoc($docentes);
$totalRows_docentes = mysql_num_rows($docentes);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/principal2.dwt.php" codeOutsideHTMLIsLocked="false" -->
<head>
<link rel="icon" type="image/x-icon" href="../imagenes/favicon.ico" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- InstanceBeginEditable name="doctitle" -->
<title>Agregar Docente</title>
<!-- InstanceEndEditable -->
<!-- InstanceBeginEditable name="head" -->
<!-- InstanceEndEditable -->
<link href="../css/twoColFixLtHdr.css" rel="stylesheet" type="text/css" />
<style type="text/css">
.container .header h1 strong {
	color: #FFF;
}
</style>
</head>

<body>

<div class="container">
  <div class="header">
    <h1>Generador de Horarios</h1>
  <!-- end .header --></div>
  <div class="sidebar1">
    <ul class="nav">
      <li><a href="../menu.php">Inicio / Menú</a></li>
    </ul>
    <p><img src="../imagenes/morroco.png" width="149" height="94" alt="morroco" /></p>
    <!-- end .sidebar1 --></div>
  <div class="content"> <!-- InstanceBeginEditable name="EditRegion3" -->
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td align="center" valign="middle"><strong>Ingreso de Docentes al Sistema</strong></td>
      </tr>
    </table>
    <br />
    <form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
      <table width="100%" align="center">
        <tr valign="baseline">
          <td width="20%" align="right" nowrap="nowrap">Nombre:</td>
          <td><input required="required" type="text" name="nombre" value="" size="50" onMouseOver="this.focus()" /></td>
        </tr>
        <tr valign="baseline">
          <td width="20%" align="right" nowrap="nowrap">&nbsp;</td>
          <td><input type="submit" value="Guardar" />
          <input name="regresar" type="button" id="regresar" onclick="window.location.href='../menu.php'" value="Regresar" /></td>
        </tr>
      </table>
      <input type="hidden" name="ID" value="<?php echo $row_max_id['MAX(docente.ID)']+1; ?>" />
      <input type="hidden" name="id_usuario" value="<?php echo $row_datos_usuario['ID']; ?>" />
      <input type="hidden" name="MM_insert" value="form1" />
    </form>
    <p>&nbsp;</p>
    <?php if ($totalRows_docentes > 0) { // Show if recordset not empty ?>
  <table width="50%" align="center">
    <tr>
      <td><strong>Docentes registrados</strong></td>
      </tr>
    <?php $var=0; do { $var=$var+1; ?>
    <tr>
      <td><?php echo $var."&nbsp;&nbsp;";  echo  $row_docentes['nombre']; ?></td>
      </tr>
    <?php } while ($row_docentes = mysql_fetch_assoc($docentes)); ?>
  </table>
      <br />
      <?php echo $totalRows_docentes ?> Docentes registrados <br />
      <?php } // Show if recordset not empty ?>
  <!-- InstanceEndEditable -->    <!-- end .content --></div>
  <div class="footer">
  <table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td align="center" valign="middle"><a href="http://www.ch0ngu1.com" target="new">www.Ch0nGu1.com</a></td>
  </tr>
</table>
    <!-- end .footer --></div>
  <!-- end .container --></div>
</body>
<!-- InstanceEnd --></html>
<?php
mysql_free_result($max_id);

mysql_free_result($datos_usuario);

mysql_free_result($docentes);
?>
